package flp.solvers;
import flp.solvers.annealing.AnnealingSettings;
import flp.solvers.annealing.AnnealingSolver;
import flp.exceptions.FlowImpossibleException;
import flp.problem.TestGenerator;
import flp.exceptions.LayoutBuildException;
import flp.problem.*;
import flp.*;
import flp.problem.ProblemInstance;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * Klasa testowa Solvera. Zawiera w sobie dośc rozbudowany przykład ProblemInstance
 * SHIFT+F6 aby wystartować test ;)
 * 
 * @author kuzu bezczelnie kopiujacy od tych genetycznych
 */
public class AnnealingSolverTest2 {

    //3 duze miasta, 25 miejsc do zabudowy
    ProblemInstance problem1;
    //Ustawienia do eksperymentowania ponizej;
    AnnealingSettings settings;


    @Before
    public void setUp() { 
        TestGenerator testGen = TestGenerator.getInstance();
        problem1 = testGen.getProblemInstance(0);
        
        //<---    TU MODYFIKOWAĆ PODCZAS TESTÓW ----> //
       
        //4. Annealing Settings
        double epsilon = 10000;                              // warunek stopu
        int layoutSize = problem1.getBuildingSitesCount();  //rozmair Layoutu
        int iterations = 100;                               //ilosc iteracji dla 1 temperatury
        int tries = 50;                                      //prob odpalenia algorytmu od nowa
        int cooling = 0;
        settings = new AnnealingSettings(layoutSize, epsilon, iterations, tries, cooling);   
    }

    /**
     * Test of computeSolution method, of class AnnealingSolver.
     */
    @Test
    public void computeSolution() throws LayoutBuildException, FlowImpossibleException { //31990 | 41005
        System.out.println("TEST METODY computeSolution\n");        
        AnnealingOptimizationMonitor monitor = new AnnealingOptimizationMonitor();
        AnnealingSolver solver = new AnnealingSolver(problem1, settings, monitor);
       
        Layout wynik=solver.computeSolution();
        assertTrue(wynik.checkCorrectness());
        System.out.println(wynik);
        
    }
    
    
    @Test
    public void findNext() throws LayoutBuildException, FlowImpossibleException {
        System.out.println("\nTEST METODY findNext\n");
        Layout x=Layout.getInstance(problem1);
        Layout y=x.clone();
        System.out.println(y);
//        System.out.println(x);
//        System.out.println(x.checkCorrectness());
//        System.out.println(x.getCost());
        AnnealingOptimizationMonitor monitor = new AnnealingOptimizationMonitor();
        AnnealingSolver solver = new AnnealingSolver(problem1, settings, monitor);
//        Layout nowy=solver.findNext(x, 10000);
//        System.out.println("nowy: "+nowy);
//       System.out.println(nowy.checkCorrectness());
//        System.out.println(nowy.getCost());
        
        double T=300000;
        while(T>10000){
            y=solver.findNext(x, T);
//             System.out.println(y);
//            if(!y.checkCorrectness()) System.out.println("zly: "+y);
            assertTrue(x.checkCorrectness());
                   
            T*=0.9;
        }
        System.out.println("ostateczny layout po paru findNext: \n"+y);
    }

    
}